package com.hxx.day6;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class TestDemo3 {
    /**
     * 使用，转换流
     */
    @Test
    public void test1(){
        //集合转流
        List<String> list = List.of("a", "b", "c");
        Stream<String> stream = list.stream();
    }
    @Test
    public void test2(){
        //数组转流
        String[] arr = {"a", "b", "c"};
        Stream<String> stream = Arrays.stream(arr);
    }
    /**
     * 通过静态方法转为流
     */
    @Test
    public void test3(){
        //通过静态方法转为流
        Stream<String> stream = Stream.of("a", "b", "c");
    }
    /**
     * 无限流
     */
    @Test
    public void test4(){
        //无限流
        Stream<String> stream1 = Stream.generate(() -> "a");
        Stream<Integer> stream = Stream.iterate(10, (x) -> x + 1);
        stream.limit(10).forEach(System.out::println);
    }
    /**
     * map可以完成类型的转换
     */
    @Test
    public void test5(){
        //map可以完成类型的转换
        Stream<String> stream = Stream.of("1", "2", "3");
        Stream<Integer> stream1 = stream.map(Integer::parseInt);
        stream1.forEach(System.out::println);
    }
    @Test
    public void test6(){
        //map可以完成类型的转换
        Stream<Student> stream = Stream.of(
                new Student("leo", 1),
                new Student("tony", 2),
                new Student("jack", 3)
                );
        //Stream<String> stringStream = stream.map(Student::getName);
        Stream<String> stringStream = stream.map((s)->s.getName());
        stringStream.forEach(System.out::println);
    }
    /**
     *  使用flatMap把每个元素转为另一个流，然后在合成一个流
     */
    @Test
    public void test7(){
        Stream<Student> stream = Stream.of(
                new Student("leo", 1),
                new Student("tony", 2),
                new Student("jack", 3)
                );
        Stream<String> stringStream = stream.flatMap((s)->Stream.of(s.getName()));
        stringStream.forEach(System.out::println);
    }

    @Test
    public void test8(){
            //创建集合
            List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
            //创建流
            list.stream()
                    .flatMap(TestDemo3::demo)
                    .forEach(System.out::println);
    }
    //把元素转为流的方法（必须是static）
    public static Stream<Integer> demo(int i){
        return Stream.of(i);
    }
    /**
     * 排序
     */
    @Test
    public void test9(){
        //创建集合
        List<Integer> list = Arrays.asList(3,1,6,2,5);
        //创建流
        list.stream()
                .sorted()
                .forEach(System.out::println);
    }
    @Test
    public void test10(){
        //创建集合
        List<Integer> list = Arrays.asList(3,1,6,2,5);
        //创建流
        list.stream()
                .sorted((x,y)->y-x)
                .forEach(System.out::println);
    }
    /**
     * 求最大值、最小值、数量
     */
    @Test
    public void test11(){
        //创建集合
        List<Integer> list = Arrays.asList(3,1,6,2,5);
        //最大值、
        Optional<Integer> max = list.stream().max((x, y) -> x - y);
        System.out.println(max.get());
        // 最小值、
        Optional<Integer> min = list.stream().min((x, y) -> x - y);
        System.out.println(min.get());
        // 数量
        long count = list.stream().count();
        System.out.println(count);
    }
    /**
     * 求总和
     */
    @Test
    public void test12(){
        //创建集合
        List<Integer> list = Arrays.asList(3,1,6,2,5);
        //总和
        Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
        System.out.println(sum.get());
    }
    @Test
    public void test13(){
        //创建集合
        List<Student> list = List.of(new Student("leo", 1),
                new Student("tony", 2),
                new Student("jack", 3));
        //求所有学生的年龄总和
       list.stream()
               .map(Student::getAge)//把每个元素转为年龄
               .reduce((x,y)->x+y)//求总和
               .ifPresent(System.out::println);
    }
}


























